angular.module('module.auth', [
    'ngCookies'
])
    .config(['$stateProvider',   '$urlRouterProvider',
        function($stateProvider, $urlRouterProvider) {
            $stateProvider
                .state('login', {
                    url : '/login',
                    userid: ['AuthService', function (AuthService) {
                        var userId = AuthService.getUserByIdCookies();
                        if(userId!=null&&userId!=''){
                            return window.history.back();
                        }
                    }],
                    templateUrl : 'view/login/login.html',
                    controller : 'LoginController'
                })
        }])
    .controller('LoginController', ['$scope','$state','$cookieStore','AuthService','ShopService',
        function($scope,$state,$cookieStore,AuthService,ShopService) {
            $scope.user = {};
            $scope.login = function (user) {
                if (!$scope.user.name) {
                    alert("请输入用户名", 1000);
                    return;
                }
                if (!$scope.user.password) {
                    alert("请输入密码", 1000);
                    return;
                };
                AuthService.userlogin(user,function(data){
                    $cookieStore.put('userid',data.userid);
                    ShopService.getByuid(data.userid).$promise.then(function(data){
                        console.log(data);
                        if(data.length>0){
                            console.log(data[0].shopid)
                            $state.go("shoplist");
                        }else{
                            alert("您还没有店铺")
                        }
                        $cookieStore.put("_shopid",data[0].shopid);
                    },function(error){
                        console.log(error)
                    })

                    //$state.go("verification");
                },function(error){
                    console.log(error);
                    alert(error.data.reason);
                })
            };
            /*var shopid = 1234;
            var data = {
                shopid:'1111',
                code:$scope.code,
            }
            AuthService.getvericode(data).$promise.then(function(data){
                var id=dada.verificationid
                $scope.code=data.code
                console.log(data.verificationid)
                AuthService.getgoodsconcrect(id).$promise.then(function(data){
                    $scope.data=data;

                })
            })*/
        }])